package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.Intelligentmanagement.common.Rest;
import com.hksj.Intelligentmanagement.dto.AnesthesiaDTO;
import com.hksj.Intelligentmanagement.entity.AnesthesiaEntity;
import com.hksj.Intelligentmanagement.mapper.AnesthesiaMapper;
import com.hksj.Intelligentmanagement.vo.AnesthesiaVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 麻醉师信息表 服务实现类
 * @author chenxingqing
 */
@Service
public class AnesthesiaService extends ServiceImpl<AnesthesiaMapper, AnesthesiaEntity>{
    

    public Rest create(AnesthesiaDTO anesthesiaDTO) {
        LambdaQueryWrapper<AnesthesiaEntity> query = new QueryWrapper<AnesthesiaEntity>().lambda()
                .eq(AnesthesiaEntity::getEmployeeNumber, anesthesiaDTO.getEmployeeNumber())
                .eq(AnesthesiaEntity::getDelFlag, 0);
        int count = count(query);
        if (count > 0){
            return Rest.fail("输入的工号已存在,请重新输入");
        }

        AnesthesiaEntity anesthesiaEntity = new AnesthesiaEntity();
        BeanUtils.copyProperties(anesthesiaDTO,anesthesiaEntity);
        anesthesiaEntity.setId(null);
        baseMapper.insert(anesthesiaEntity);
        return Rest.ok();
    }

    public Rest updateAnesthesia(AnesthesiaDTO anesthesiaDTO) {
        LambdaQueryWrapper<AnesthesiaEntity> query = new QueryWrapper<AnesthesiaEntity>().lambda()
                .eq(AnesthesiaEntity::getEmployeeNumber, anesthesiaDTO.getEmployeeNumber())
                .eq(AnesthesiaEntity::getDelFlag, 0)
                .ne(AnesthesiaEntity::getId,anesthesiaDTO.getId());
        int count = count(query);
        if (count > 0){
            return Rest.fail("输入的工号已存在,请重新输入");
        }

        AnesthesiaEntity anesthesiaEntity = new AnesthesiaEntity();
        BeanUtils.copyProperties(anesthesiaDTO,anesthesiaEntity);
        baseMapper.updateById(anesthesiaEntity);
        return Rest.ok();
    }

    public Rest delete(String id) {
        AnesthesiaEntity anesthesiaEntity = new AnesthesiaEntity();
        anesthesiaEntity.setId(id);
        anesthesiaEntity.setDelFlag(1);
        baseMapper.updateById(anesthesiaEntity);
        return Rest.ok();
    }

    public List<AnesthesiaVo> listAnesthesia(AnesthesiaDTO anesthesiaDTO) {
        List<AnesthesiaVo> anesthesiaVos = baseMapper.listAnesthesia(anesthesiaDTO);
        return anesthesiaVos;
    }

    public IPage<AnesthesiaVo> pageAnesthesia(Page<AnesthesiaVo> page, String anesthesiaName, String employeeNumber, String status) {
        List<AnesthesiaVo> anesthesiaVos = baseMapper.pageAnesthesia(page,anesthesiaName,employeeNumber,status);
        page.setRecords(anesthesiaVos);
        return page;
    }
}
